package elitech.vietnam.myfashion.extend;

import java.io.IOException;
import java.util.List;

import org.xmlpull.v1.XmlPullParserException;

import android.content.Context;
import android.os.Bundle;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView.LayoutParams;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.koushikdutta.urlimageviewhelper.UrlImageViewHelper;

import elitech.vietnam.myfashion.MainActivity;
import elitech.vietnam.myfashion.MyApplication;
import elitech.vietnam.myfashion.R;
import elitech.vietnam.myfashion.config.Const;
import elitech.vietnam.myfashion.entites.Notify;
import elitech.vietnam.myfashion.fragment.DetailFragment;
import elitech.vietnam.myfashion.fragment.FollowListFragment;
import elitech.vietnam.myfashion.fragment.StylerFragment;
import elitech.vietnam.myfashion.gcm.GCMIntentService;
import elitech.vietnam.myfashion.gcm.GCMIntentService.GCMEntity;
import elitech.vietnam.myfashion.utilities.TimeDiff;
import elitech.vietnam.myfashion.wsclient.Service;

public class NotifyArrayAdapter extends ArrayAdapter<Notify> {
	
	LayoutParams mLParam;
	MainActivity mActivity;
	
	public NotifyArrayAdapter(Context context, int resource, List<Notify> objects) {
		super(context, resource, objects);
		mLParam = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
		mActivity = (MainActivity) getContext();
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View row = convertView;
		ViewHolder holder;
		
		if (row == null) {
			LayoutInflater inflater = LayoutInflater.from(getContext());
			row = inflater.inflate(R.layout.notification_list_item, parent, false);
			
			holder = new ViewHolder();
			holder.image = (ImageView) row.findViewById(R.id.notiitem_image);
			holder.title = (TextView) row.findViewById(R.id.notiitem_content);
			holder.time = (TextView) row.findViewById(R.id.notiitem_time);

			row.setTag(holder);
		} else
			holder = (ViewHolder) row.getTag();
		
		final Notify noti = getItem(position);
		GCMEntity e = GCMEntity.newInstance(getContext(), noti);
		holder.title.setText(Html.fromHtml(e.bigtext));
		UrlImageViewHelper.setUrlDrawable(holder.image, Const.SERVER_IMAGE_URL + noti.Image, R.drawable.no_avatar);
		holder.time.setText(TimeDiff.getTimeDiffString(noti.Date(), getContext()));
		if (noti.Unread())
			row.setBackgroundResource(R.drawable.item_notification_unread);
		else
			row.setBackgroundResource(R.drawable.item_notification);
		
		row.setLayoutParams(mLParam);
		
		row.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				if (noti.isNew) {
					((MyApplication) mActivity.getApplicationContext()).mBadge.increaseBadge(-1);
					mActivity.mPrefs.edit().putInt("mBadgeCount", ((MyApplication) mActivity.getApplicationContext()).mBadge.mBadgeCount).commit();
					noti.isNew = false;
				}
				noti.Unread = 0;
				if (noti.Type == 1 || noti.Type == 2 || noti.Type == 3) {
					final boolean myStyle = (mActivity.mUser != null && mActivity.mUser.Id == noti.MemID);
					StylerFragment fragment = new StylerFragment();
					fragment.setPostID(noti.PostID, myStyle);
					mActivity.replaceFragment(fragment, true);
					new SetNotificationUnread().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, noti.Id);
				}
				if (noti.Type == 4) {
					if (mActivity.mFollowListFragment == null)
						mActivity.mFollowListFragment = new FollowListFragment();
					Bundle bundle = new Bundle();
					bundle.putInt("TAB", 1);
					mActivity.mFollowListFragment.setArguments(bundle);
					if (!mActivity.mFollowListFragment.isAdded()) {
						mActivity.replaceFragment(mActivity.mFollowListFragment, true);
					}
					new SetNotificationUnread().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, noti.Id);
				}
				if (noti.Type == 5) {
					DetailFragment fragment = new DetailFragment();
					fragment.setProductID(noti.PostID);
					mActivity.replaceFragment(fragment, true);
					new SetNotificationUnread().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, noti.Id);
				}
				GCMIntentService.NUMBER = 0;
			}
		});
		
		return row;
	}
	
	static class ViewHolder {
		ImageView image;
		TextView title, time;
	}

	static class SetNotificationUnread extends AsyncTask<Integer, Integer, Boolean> {
		@Override
		protected Boolean doInBackground(Integer... params) {
			try {
				new Service().readNotification(params[0]);
			} catch (IOException | XmlPullParserException e) {
				e.printStackTrace();
			}
			return null;
		}
	}
}
